<template>
  <div id="wuran" class="wuran" style="width: 90%; height: 300px;" />
</template>

<script>
import * as echarts from 'echarts'

export default {
  name: 'WuranChart',
  mounted() {
    this.renderChart()
  },
  methods: {
    renderChart() {
      const myChart = echarts.init(document.getElementById('wuran'))
      const salvProName = ['茶', '养殖', '水稻', '设施农业', '数字乡村']
      const salvProValue = [117, 74, 72, 67, 55]
      const salvProMax = []// 背景按最大值
      for (let i = 0; i < salvProValue.length; i++) {
        salvProMax.push(salvProValue[0])
      }
      const option = {
        grid: {
          left: '2%',
          right: '2%',
          bottom: '-6%',
          top: '8%',
          containLabel: true
        },
        tooltip: {
          trigger: 'axis',
          axisPointer: {
            type: 'none'
          },
          formatter: function(params) {
            return params[0].name + ' : ' + params[0].value
          }
        },
        xAxis: {
          show: false,
          type: 'value'
        },
        yAxis: [{
          type: 'category',
          inverse: true,
          axisLabel: {
            show: true,
            textStyle: {
              color: '#fff'
            }
          },
          splitLine: {
            show: false
          },
          axisTick: {
            show: false
          },
          axisLine: {
            show: false
          },
          data: salvProName
        }, {
          type: 'category',
          inverse: true,
          axisTick: 'none',
          axisLine: 'none',
          show: true,
          axisLabel: {
            textStyle: {
              color: '#ffffff',
              fontSize: '12'
            }
          },
          data: salvProValue
        }],
        series: [{
          name: '值',
          type: 'bar',
          zlevel: 1,
          itemStyle: {
            normal: {
              barBorderRadius: 30,
              color: new echarts.graphic.LinearGradient(0, 0, 1, 0, [{
                offset: 0,
                color: 'rgb(57,89,255,1)'
              }, {
                offset: 1,
                color: 'rgb(46,200,207,1)'
              }])
            }
          },
          barWidth: 10,
          data: salvProValue
        },
        {
          name: '背景',
          type: 'bar',
          barWidth: 10,
          barGap: '-100%',
          data: salvProMax,
          itemStyle: {
            normal: {
              color: 'rgba(24,31,68,1)',
              barBorderRadius: 30
            }
          }
        }
        ]
      }
      myChart.setOption(option)
    }
  }
}
</script>

<style scoped lang='scss'>
 .wuran {
  padding-left: 20px;
 }
</style>
